Migration guide for Tensor update¶
In SlangPy version 0.41 we undertook a major rewrite of the Tensor API to bring it inline with existing standards and simplify its use going forwards. The primary changes made are:
Python API¶
NDBufferis fully deprecatedTensoris the sole ND container type, and supports both differentiable and non-differentiable data
Slang API¶
NDBufferis removedTensorandRWTensortypes now only store non-differentiable dataWTensorintroduced to store write-only non-differentiable dataDiffTensor,WDiffTensorandRWDiffTensorintroduced to store differentiable datagetandsetmethods replaced withloadandstoremethods respectivelygetvandsetvmethods replaced withloadandstoremethods respectivelysubscript operators correctly implemented for all Tensors
Migrating¶
Most migration steps can be automated using search-and-replace, however doing them in the correct order is important to avoid naming issues.
1. Remove use of NDBuffer in Python code¶
Replace all instances of
NDBufferwithTensorFix any use of the
NDBufferconstructor to useTensor.empty
Note: Use of NDBuffer constructor with positional arguments may not be easily fixable with search-and-replace. It is probably worth reviewing these instances manually.
2. Replace differentiable Tensors parameters in Slang¶
If you have exclusively used the Tensor type as function parameters, simply search and replacing as below should be sufficient.
Replace all instances of
TensorwithIDiffTensorReplace all instances of
RWTensorwithIRWDiffTensorReplace all instances of
GradInTensorwithIWDiffTensorReplace all instances of
GradOutTensorwithIDiffTensorReplace all instances of
GradInOutTensorwithIRWDiffTensor
Where tensors have been used as variables, it can depend on the use case. Typically:
- Tensor/RWTensor will typically be either:
Keep the same if using purely as ND storage
Replace with PrimalTensor/ RWPrimalTensor if you need it to be compatible with IDiffTensor interfaces
GradInTensor should be changed to WDiffTensor
GradOutTensor should be changed to DiffTensor
GradInOutTensor should be changed to RWDiffTensor
3. Replace NDBuffers with non-differentiable Tensors in Slang code¶
For function arguments:
Replace all instances of
NDBufferwithITensorReplace all instances of
RWWBufferwithIRWTensor
Where NDBuffers have been used as variables:
Replace all instances of
NDBufferwithTensorReplace all instances of
RWBufferwithRWTensor
4. Fix get/set and getv/setv method calls¶
Replace all instances of
.get(with.load(Replace all instances of
.set(with.store(Replace all instances of
.getv(with.load(Replace all instances of
.setv(with.store(
5. Fix any errors attempting to access gradient buffers in Slang¶
If Slang has compile errors attempting to access the ‘grad’ properties of tensors, switch them from using IDiffTensor to the concrete DiffTensor (or corresponding) types.