Workspace

class dragon.Workspace(name='')[source]

Sandbox to isolate the resources and computations.

__init__

Workspace.__init__(name='')[source]

Create a Workspace.

Parameters:
  • name (str, optional, default='') – The optional workspace name.

Methods

as_default

Workspace.as_default()[source]

Switch this workspace as the default.

Call this method with the with keyword.

Once with is exited, the previous default will be set.

Returns:
dragon.Workspace – This workspace.

clear

Workspace.clear()[source]

Clear the cached tensors, operators and graphs.

Call this method before deleting to free cached resources certainly:

my_workspace = dragon.Workspace()
my_workspace.clear()
del my_workspace

feed_tensor

Workspace.feed_tensor(
  tensor,
  value,
  dtype=None,
  enforce_cpu=False
)[source]

Copy the value to tensor.

Examples:

# Define a named tensor to feed
x = dragon.Tensor(name='x')
dragon.get_workspace().feed_tensor(x, 0)

# Feed by specifying a tensor name
# Note that it will create the implementation whatever
dragon.get_workspace().feed_tensor('y', 1)
print(dragon.get_workspace().has_tensor('y'))  # True
Parameters:
  • tensor (Union[dragon.Tensor, str]) – The tensor to feed.
  • value (array_like) – The value to copy.
  • dtype (str, optional) – The optional data type.
  • enforce_cpu (bool, optional, default=False) – True to copy using cpu context.

fetch_tensor

Workspace.fetch_tensor(tensor)[source]

Return the value of tensor.

Parameters:
Returns:

numpy.ndarray – The array copied from backend.

has_tensor

Workspace.has_tensor(tensor)[source]

Return whether the tensor is in this workspace.

Parameters:
Returns:

boolTrue if tensor is existing otherwise False.

memory_allocated

Workspace.memory_allocated(
  device_type='cpu',
  device_index=0
)[source]

Return the size of memory used by tensors on given device.

Parameters:
  • device_type (str, optional) – The device type.
  • device_index (int, optional) – The device index.
Returns:

int – The total number of allocated bytes.

merge_from

Workspace.merge_from(other)[source]

Merge resources from the other.

The other will not be reset until self is reset.

Parameters:
Returns:

dragon.Workspace – This workspace.

reset_tensor

Workspace.reset_tensor(tensor)[source]

Reset the tensor.

Parameters: