torch.memory.scopeDepth
function scopeDepth(): numberReturns the current memory scope nesting depth.
Indicates how many nested torch.scope() blocks are currently active.
Useful for debugging, logging, and understanding scope structure.
Return Values:
0: At global level (no scopes active)1: Inside one scope2+: Nested scopes
Use Cases:
- Debug scope nesting issues
- Log scope information
- Implement scope-aware debugging
- Verify scope structure in complex code
- Constant-time operation: O(1) lookup
- Read-only: Doesn't change any state
- For debugging: Primarily useful for debugging and logging
Returns
number– Current scope nesting depth (0 at global level)Examples
// At global level
torch.memory.scopeDepth(); // 0
// Nested scopes
torch.scope(() => {
console.log(torch.memory.scopeDepth()); // 1
torch.scope(() => {
console.log(torch.memory.scopeDepth()); // 2
torch.scope(() => {
console.log(torch.memory.scopeDepth()); // 3
});
console.log(torch.memory.scopeDepth()); // 2 again
});
console.log(torch.memory.scopeDepth()); // 1 again
});
console.log(torch.memory.scopeDepth()); // 0 again
// Debugging helper
function debugTensorCreation(name: string, tensor: Tensor) {
const depth = torch.memory.scopeDepth();
const indent = ' '.repeat(depth);
console.log(`${indent}Created ${name} at scope depth ${depth}`);
return tensor;
}See Also
- [PyTorch N/A (torch.js specific)](https://pytorch.org/docs/stable/generated/N/A .html)
- scope - Create a memory scope
- inScope - Check if inside any scope
- stats - Get detailed memory statistics