MCPcopy Create free account
hub / github.com/pytorch/pytorch / get_buffer

Method get_buffer

torch/nn/modules/module.py:728–761  ·  view source on GitHub ↗

Return the buffer given by ``target`` if it exists, otherwise throw an error. See the docstring for ``get_submodule`` for a more detailed explanation of this method's functionality as well as how to correctly specify ``target``. Args: target: The fully-q

(self, target: str)

Source from the content-addressed store, hash-verified

726 return param
727
728 def get_buffer(self, target: str) -> "Tensor":
729 """Return the buffer given by ``target`` if it exists, otherwise throw an error.
730
731 See the docstring for ``get_submodule`` for a more detailed
732 explanation of this method's functionality as well as how to
733 correctly specify ``target``.
734
735 Args:
736 target: The fully-qualified string name of the buffer
737 to look for. (See ``get_submodule`` for how to specify a
738 fully-qualified string.)
739
740 Returns:
741 torch.Tensor: The buffer referenced by ``target``
742
743 Raises:
744 AttributeError: If the target string references an invalid
745 path or resolves to something that is not a
746 buffer
747 """
748 module_path, _, buffer_name = target.rpartition(".")
749
750 mod: torch.nn.Module = self.get_submodule(module_path)
751
752 if not hasattr(mod, buffer_name):
753 raise AttributeError(mod._get_name() + " has no attribute `"
754 + buffer_name + "`")
755
756 buffer: torch.Tensor = getattr(mod, buffer_name)
757
758 if buffer_name not in mod._buffers:
759 raise AttributeError("`" + buffer_name + "` is not a buffer")
760
761 return buffer
762
763 def get_extra_state(self) -> Any:
764 """Return any extra state to include in the module's state_dict.

Calls 2

get_submoduleMethod · 0.95
_get_nameMethod · 0.45