org.nd4j.linalg.factory.Nd4j.getWorkspaceManager()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(9.1k)|赞(0)|评价(0)|浏览(206)

本文整理了Java中org.nd4j.linalg.factory.Nd4j.getWorkspaceManager()方法的一些代码示例,展示了Nd4j.getWorkspaceManager()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Nd4j.getWorkspaceManager()方法的具体详情如下:
包路径:org.nd4j.linalg.factory.Nd4j
类名称:Nd4j
方法名:getWorkspaceManager

Nd4j.getWorkspaceManager介绍

[英]This method returns WorkspaceManager implementation to be used within this JVM process
[中]此方法返回要在此JVM进程中使用的WorkspaceManager实现

代码示例

代码示例来源:origin: deeplearning4j/nd4j

  1. /**
  2. * Assert that the specified workspace is open and active
  3. *
  4. * @param ws Name of the workspace to assert open and active
  5. * @param errorMsg Message to include in the exception, if required
  6. */
  7. public static void assertOpenAndActive(@NonNull String ws, @NonNull String errorMsg) throws ND4JWorkspaceException {
  8. if (!Nd4j.getWorkspaceManager().checkIfWorkspaceExistsAndActive(ws)) {
  9. throw new ND4JWorkspaceException(errorMsg);
  10. }
  11. }

代码示例来源:origin: deeplearning4j/nd4j

  1. private void enforceExistsAndActive(@NonNull T arrayType){
  2. validateConfig(arrayType);
  3. if(scopeOutOfWs.contains(arrayType)){
  4. return;
  5. }
  6. if(!Nd4j.getWorkspaceManager().checkIfWorkspaceExistsAndActive(workspaceNames.get(arrayType))){
  7. throw new ND4JWorkspaceException("Workspace \"" + workspaceNames.get(arrayType) + "\" for array type " + arrayType
  8. + " is not open");
  9. }
  10. }
  11. }

代码示例来源:origin: deeplearning4j/nd4j

  1. private static List<String> allOpenWorkspaces(){
  2. List<MemoryWorkspace> l = Nd4j.getWorkspaceManager().getAllWorkspacesForCurrentThread();
  3. List<String> workspaces = new ArrayList<>(l.size());
  4. for( MemoryWorkspace ws : l){
  5. if(ws.isScopeActive()) {
  6. workspaces.add(ws.getId());
  7. }
  8. }
  9. return workspaces;
  10. }

代码示例来源:origin: deeplearning4j/nd4j

  1. private static List<String> allOpenWorkspaces(){
  2. List<MemoryWorkspace> l = Nd4j.getWorkspaceManager().getAllWorkspacesForCurrentThread();
  3. List<String> workspaces = new ArrayList<>(l.size());
  4. for( MemoryWorkspace ws : l){
  5. if(ws.isScopeActive()) {
  6. workspaces.add(ws.getId());
  7. }
  8. }
  9. return workspaces;
  10. }
  11. }

代码示例来源:origin: deeplearning4j/nd4j

  1. @Override
  2. public boolean isWorkspaceOpen(@NonNull T arrayType) {
  3. validateConfig(arrayType);
  4. if(!scopeOutOfWs.contains(arrayType)) {
  5. return Nd4j.getWorkspaceManager().checkIfWorkspaceExistsAndActive(getWorkspaceName(arrayType));
  6. }
  7. return true;
  8. }

代码示例来源:origin: deeplearning4j/nd4j

  1. /**
  2. * Assert that no workspaces are currently open
  3. *
  4. * @param msg Message to include in the exception, if required
  5. */
  6. public static void assertNoWorkspacesOpen(String msg) throws ND4JWorkspaceException {
  7. if (Nd4j.getWorkspaceManager().anyWorkspaceActiveForCurrentThread()) {
  8. List<MemoryWorkspace> l = Nd4j.getWorkspaceManager().getAllWorkspacesForCurrentThread();
  9. List<String> workspaces = new ArrayList<>(l.size());
  10. for (MemoryWorkspace ws : l) {
  11. if(ws.isScopeActive()) {
  12. workspaces.add(ws.getId());
  13. }
  14. }
  15. throw new ND4JWorkspaceException(msg + " - Open/active workspaces: " + workspaces);
  16. }
  17. }

代码示例来源:origin: deeplearning4j/nd4j

  1. @Override
  2. public MemoryWorkspace notifyScopeBorrowed(@NonNull T arrayType) {
  3. validateConfig(arrayType);
  4. enforceExistsAndActive(arrayType);
  5. if(scopeOutOfWs.contains(arrayType)){
  6. return Nd4j.getWorkspaceManager().scopeOutOfWorkspaces();
  7. } else {
  8. MemoryWorkspace ws = Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread(
  9. getConfiguration(arrayType), getWorkspaceName(arrayType));
  10. return ws.notifyScopeBorrowed();
  11. }
  12. }

代码示例来源:origin: deeplearning4j/nd4j

  1. @Override
  2. public MemoryWorkspace notifyScopeEntered(@NonNull T arrayType) {
  3. validateConfig(arrayType);
  4. if(isScopedOut(arrayType)){
  5. return Nd4j.getWorkspaceManager().scopeOutOfWorkspaces();
  6. } else {
  7. MemoryWorkspace ws = Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread(
  8. getConfiguration(arrayType), getWorkspaceName(arrayType));
  9. return ws.notifyScopeEntered();
  10. }
  11. }

代码示例来源:origin: deeplearning4j/nd4j

  1. /**
  2. * Assert that the specified workspace is open, active, and is the current workspace
  3. *
  4. * @param ws Name of the workspace to assert open/active/current
  5. * @param errorMsg Message to include in the exception, if required
  6. */
  7. public static void assertOpenActiveAndCurrent(@NonNull String ws, @NonNull String errorMsg) throws ND4JWorkspaceException {
  8. if (!Nd4j.getWorkspaceManager().checkIfWorkspaceExistsAndActive(ws)) {
  9. throw new ND4JWorkspaceException(errorMsg + " - workspace is not open and active");
  10. }
  11. MemoryWorkspace currWs = Nd4j.getMemoryManager().getCurrentWorkspace();
  12. if (currWs == null || !ws.equals(currWs.getId())) {
  13. throw new ND4JWorkspaceException(errorMsg + " - not the current workspace (current workspace: "
  14. + (currWs == null ? null : currWs.getId()));
  15. }
  16. }

代码示例来源:origin: deeplearning4j/nd4j

  1. private void initWorkspace() {
  2. workspace = Nd4j.getWorkspaceManager().createNewWorkspace(
  3. WorkspaceConfiguration.builder()
  4. .initialSize(memoryForGraph())
  5. .policyAllocation(AllocationPolicy.OVERALLOCATE)
  6. .policyLearning(LearningPolicy.FIRST_LOOP)
  7. .build());
  8. Nd4j.getWorkspaceManager().setWorkspaceForCurrentThread(workspace);
  9. }

代码示例来源:origin: deeplearning4j/nd4j

  1. /**
  2. * This method detaches INDArray from current Workspace, and attaches it to Workspace with a given Id, if a workspace
  3. * with the given ID is open and active.
  4. *
  5. * If the workspace does not exist, or is not active, the array is detached from any workspaces.
  6. *
  7. * @param id ID of the workspace to leverage to
  8. * @return The INDArray, leveraged to the specified workspace (if it exists and is active) otherwise the detached array
  9. * @see #leverageTo(String)
  10. */
  11. public INDArray leverageOrDetach(String id){
  12. if(!isAttached()){
  13. return this;
  14. }
  15. if(!Nd4j.getWorkspaceManager().checkIfWorkspaceExistsAndActive(id)){
  16. return detach();
  17. }
  18. return leverageTo(id);
  19. }

代码示例来源:origin: deeplearning4j/nd4j

  1. this.id = workspaceId;
  2. this.threadId = Thread.currentThread().getId();
  3. this.guid = Nd4j.getWorkspaceManager().getUUID();
  4. this.memoryManager = Nd4j.getMemoryManager();
  5. this.deviceId = Nd4j.getAffinityManager().getDeviceForCurrentThread();

代码示例来源:origin: deeplearning4j/dl4j-examples

  1. try(MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(initialConfig, "SOME_ID")) {
  2. try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(learningConfig, "OTHER_ID")) {
  3. INDArray array = Nd4j.create(100);
  4. try(MemoryWorkspace ws1 = Nd4j.getWorkspaceManager().getAndActivateWorkspace(initialConfig, "SOME_ID")) {
  5. INDArray array = Nd4j.create(10, 10).assign(1.0f);
  6. INDArray sumRes;
  7. try(MemoryWorkspace ws2 = Nd4j.getWorkspaceManager().getAndActivateWorkspace(initialConfig, "THIRD_ID")) {
  8. try(MemoryWorkspace ws1 = Nd4j.getWorkspaceManager().getAndActivateWorkspace(initialConfig, "SOME_ID")) {
  9. INDArray array1 = Nd4j.create(10, 10).assign(1.0f);
  10. INDArray array2;
  11. try(MemoryWorkspace ws = Nd4j.getWorkspaceManager().scopeOutOfWorkspaces()) {
  12. try (MemoryWorkspace ws1 = Nd4j.getWorkspaceManager().getAndActivateWorkspace(circularConfig, "CIRCULAR_ID")) {
  13. INDArray array = Nd4j.create(100);

代码示例来源:origin: deeplearning4j/nd4j

  1. @Override
  2. public INDArray getActivation(INDArray in, boolean training) {
  3. if (training) {
  4. try(MemoryWorkspace ws = Nd4j.getWorkspaceManager().scopeOutOfWorkspaces()) {
  5. this.alpha = Nd4j.rand(in.shape(), l, u, Nd4j.getRandom());
  6. }
  7. INDArray inTimesAlpha = in.mul(alpha);
  8. BooleanIndexing.replaceWhere(in, inTimesAlpha, Conditions.lessThan(0));
  9. } else {
  10. this.alpha = null;
  11. double a = 0.5 * (l + u);
  12. return Nd4j.getExecutioner().execAndReturn(new RectifedLinear(in, a));
  13. }
  14. return in;
  15. }

代码示例来源:origin: deeplearning4j/nd4j

  1. return this;
  2. if (!Nd4j.getWorkspaceManager().checkIfWorkspaceExists(id)) {
  3. if(enforceExistence){
  4. throw new Nd4jNoSuchWorkspaceException(id);
  5. MemoryWorkspace target = Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread(id);

代码示例来源:origin: org.nd4j/nd4j-cuda-10.0

  1. @Override
  2. public MemoryWorkspace getWorkspaceForCurrentThread(@NonNull WorkspaceConfiguration configuration, @NonNull String id) {
  3. ensureThreadExistense();
  4. MemoryWorkspace workspace = backingMap.get().get(id);
  5. if (workspace == null) {
  6. workspace = newWorkspace(configuration, id);
  7. backingMap.get().put(id, workspace);
  8. if (Nd4j.getWorkspaceManager().getDebugMode() != DebugMode.BYPASS_EVERYTHING)
  9. pickReference(workspace);
  10. }
  11. return workspace;
  12. }

代码示例来源:origin: org.nd4j/nd4j-cuda-10.0

  1. @Override
  2. public MemoryWorkspace createNewWorkspace(WorkspaceConfiguration configuration, String id) {
  3. ensureThreadExistense();
  4. MemoryWorkspace workspace = newWorkspace(configuration, id);
  5. backingMap.get().put(id, workspace);
  6. if (Nd4j.getWorkspaceManager().getDebugMode() != DebugMode.BYPASS_EVERYTHING)
  7. pickReference(workspace);
  8. return workspace;
  9. }

代码示例来源:origin: org.nd4j/nd4j-cuda-10.0

  1. @Override
  2. public MemoryWorkspace createNewWorkspace(WorkspaceConfiguration configuration, String id, Integer deviceId) {
  3. ensureThreadExistense();
  4. MemoryWorkspace workspace = newWorkspace(configuration, id, deviceId);
  5. backingMap.get().put(id, workspace);
  6. if (Nd4j.getWorkspaceManager().getDebugMode() != DebugMode.BYPASS_EVERYTHING)
  7. pickReference(workspace);
  8. return workspace;
  9. }

代码示例来源:origin: org.nd4j/nd4j-cuda-10.0

  1. @Override
  2. public MemoryWorkspace createNewWorkspace(@NonNull WorkspaceConfiguration configuration) {
  3. ensureThreadExistense();
  4. MemoryWorkspace workspace = newWorkspace(configuration);
  5. backingMap.get().put(workspace.getId(), workspace);
  6. if (Nd4j.getWorkspaceManager().getDebugMode() != DebugMode.BYPASS_EVERYTHING)
  7. pickReference(workspace);
  8. return workspace;
  9. }

代码示例来源:origin: org.nd4j/nd4j-cuda-10.0

  1. @Override
  2. public MemoryWorkspace createNewWorkspace() {
  3. ensureThreadExistense();
  4. MemoryWorkspace workspace = newWorkspace(defaultConfiguration);
  5. backingMap.get().put(workspace.getId(), workspace);
  6. if (Nd4j.getWorkspaceManager().getDebugMode() != DebugMode.BYPASS_EVERYTHING)
  7. pickReference(workspace);
  8. return workspace;
  9. }

相关文章