Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions tensordict/nn/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
except ImportError:
_has_functorch = False

class FunctionalModule:
pass

class FunctionalModuleWithBuffers:
pass


__all__ = [
"TensorDictModule",
"TensorDictModuleWrapper",
Expand Down Expand Up @@ -153,7 +160,12 @@ def __init__(
def is_functional(self):
return isinstance(
self.module,
(functorch.FunctionalModule, functorch.FunctionalModuleWithBuffers),
(
FunctionalModule,
FunctionalModuleWithBuffers,
rlFunctionalModule,
rlFunctionalModuleWithBuffers,
),
)

def _write_to_tensordict(
Expand Down Expand Up @@ -408,15 +420,15 @@ def make_functional_with_buffers(self, clone: bool = True, native: bool = False)
def num_params(self):
if isinstance(
self.module,
(functorch.FunctionalModule, functorch.FunctionalModuleWithBuffers),
(FunctionalModule, FunctionalModuleWithBuffers),
):
return len(self.module.param_names)
else:
return 0

@property
def num_buffers(self):
if isinstance(self.module, (functorch.FunctionalModuleWithBuffers,)):
if isinstance(self.module, FunctionalModuleWithBuffers):
return len(self.module.buffer_names)
else:
return 0
Expand Down
3 changes: 2 additions & 1 deletion tensordict/nn/sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ def make_functional_with_buffers(self, clone: bool = True, native: bool = False)
is_shared=False)

"""
native = native or not _has_functorch
if clone:
self_copy = deepcopy(self)
self_copy.module = copy(self_copy.module)
Expand All @@ -406,7 +407,7 @@ def make_functional_with_buffers(self, clone: bool = True, native: bool = False)
_params,
_buffers,
) = module.make_functional_with_buffers(clone=True, native=native)
if native or not _has_functorch:
if native:
params[str(i)] = _params
buffers[str(i)] = _buffers
else:
Expand Down