You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

tensor.py 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
  5. #
  6. # Unless required by applicable law or agreed to in writing,
  7. # software distributed under the License is distributed on an
  8. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. import collections
  10. from .core import Tensor as _Tensor
  11. from .core.ops.builtin import Copy
  12. from .core.tensor.core import apply
  13. from .core.tensor.raw_tensor import as_device
  14. from .device import _valid_device, get_default_device
  15. from .utils.deprecation import deprecated
  16. class Tensor(_Tensor):
  17. grad = None
  18. dmap_callback = None
  19. def __init__(self, data, dtype=None, device=None):
  20. if device is None:
  21. device = get_default_device()
  22. self.q_dict = {"mode": None, "scale": None, "zero_point": None}
  23. super().__init__(data, dtype=dtype, device=device)
  24. @deprecated(version="1.0", reason="no need to reuse an existing tensor since 1.0")
  25. def set_value(self, value):
  26. self._reset(value)
  27. @deprecated(version="1.0", reason="use *= 0 instead")
  28. def reset_zero(self):
  29. self *= 0
  30. def to(self, device):
  31. if isinstance(device, str) and not _valid_device(device):
  32. raise ValueError(
  33. "invalid device name {}. For the correct format of the device name, please refer to the instruction of megengine.device.set_default_device()".format(
  34. device
  35. )
  36. )
  37. cn = as_device(device).to_c()
  38. return apply(Copy(comp_node=cn), self)[0]
  39. @property
  40. def requires_grad(self):
  41. raise AttributeError("requires_grad is reserved for future use")
  42. @requires_grad.setter
  43. def requires_grad(self, value):
  44. raise AttributeError("requires_grad is reserved for future use")
  45. @requires_grad.deleter
  46. def requires_grad(self):
  47. raise AttributeError("requires_grad is reserved for future use")
  48. def __hash__(self):
  49. return id(self)
  50. def __getstate__(self):
  51. r""" __getstate__ will be called for pickle serialization or deep copy
  52. """
  53. state = {
  54. "data": self.numpy(),
  55. "device": self.device.logical_name,
  56. "dtype": self.dtype,
  57. "qdict": self.q_dict,
  58. }
  59. return state
  60. def __setstate__(self, state):
  61. data = state.pop("data")
  62. logical_device = state.pop("device")
  63. if self.dmap_callback is not None:
  64. assert isinstance(logical_device, str)
  65. logical_device = self.dmap_callback(logical_device)
  66. dtype = state.pop("dtype")
  67. self.q_dict = state.pop("qdict")
  68. super().__init__(data, dtype=dtype, device=logical_device)
  69. def detach(self):
  70. r"""
  71. Returns a new tensor which is treated as constant during backward gradient calcuation,
  72. i.e. its gradient is zero.
  73. :param inp: input tensor
  74. """
  75. Wrapper = type(self)
  76. Tensor = type(self.__wrapped__)
  77. return Wrapper(Tensor(self.__wrapped__._data))
  78. tensor = Tensor
  79. class Parameter(Tensor):
  80. r"""A kind of Tensor that is to be considered a module parameter.
  81. """

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台