1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
|
#include <torch/csrc/lazy/core/config.h>
#include <torch/csrc/lazy/core/tensor.h>
#include <c10/util/irange.h>
#include <torch/csrc/lazy/core/helpers.h>
#include <torch/csrc/lazy/core/ir_builder.h>
#include <torch/csrc/lazy/core/ir_dump_util.h>
#include <torch/csrc/lazy/core/lazy_graph_executor.h>
#include <torch/csrc/lazy/core/metrics.h>
#include <torch/csrc/lazy/core/tensor_impl.h>
#include <torch/csrc/lazy/core/tensor_util.h>
#include <ATen/FunctionalTensorWrapper.h>
#include <utility>
namespace torch::lazy {
namespace {
LazyTensorPtr GetOrCreateLtcTensor(
const at::Tensor& tensor,
const BackendDevice& device) {
if (!tensor.defined()) {
return torch::lazy::LazyTensorPtr();
}
auto lazy_tensor = TryGetLtcTensor(tensor);
return lazy_tensor ? lazy_tensor : LazyTensor::Create(tensor, device);
}
} // namespace
LazyTensor::Data::~Data() {
LazyGraphExecutor::Get()->UnregisterTensor(this);
}
LazyTensorPtr LazyTensor::Create(
const at::Tensor& tensor,
const BackendDevice& device) {
TORCH_CHECK(tensor.device().type() != at::kLazy);
LazyTensorPtr lazy_tensor =
c10::make_intrusive<LazyTensor>(LazyTensor(tensor, device));
LazyGraphExecutor::Get()->RegisterTensor(lazy_tensor->data());
return lazy_tensor;
}
LazyTensorPtr LazyTensor::Create(Value ir_value, const BackendDevice& device) {
LazyTensorPtr lazy_tensor =
c10::make_intrusive<LazyTensor>(LazyTensor(std::move(ir_value), device));
LazyGraphExecutor::Get()->RegisterTensor(lazy_tensor->data());
return lazy_tensor;
}
LazyTensorPtr LazyTensor::Create(const BackendDataPtr& handle) {
LazyTensorPtr lazy_tensor =
c10::make_intrusive<LazyTensor>(LazyTensor(handle));
LazyGraphExecutor::Get()->RegisterTensor(lazy_tensor->data());
return lazy_tensor;
}
LazyTensorPtr LazyTensor::Create(std::shared_ptr<Data> data) {
return c10::make_intrusive<LazyTensor>(LazyTensor(std::move(data)));
}
LazyTensor::LazyTensor(const at::Tensor& tensor, const BackendDevice& device)
: LazyTensor(std::make_shared<Data>(tensor, device)) {}
LazyTensor::LazyTensor(const BackendDataPtr& handle)
: LazyTensor(std::make_shared<Data>(handle, handle->device())) {}
LazyTensor::LazyTensor(Value ir_value, const BackendDevice& device)
: LazyTensor(std::make_shared<Data>(std::move(ir_value), device)) {
TryLimitGraphSize();
}
LazyTensor::LazyTensor(std::shared_ptr<Data> data) : data_(std::move(data)) {}
auto LazyTensor::data() const -> const std::shared_ptr<Data>& {
TORCH_CHECK(data_ != nullptr, "Trying to access a null cursor");
return data_;
}
int64_t LazyTensor::size(int64_t dim) const {
auto tensor_shape = shape();
auto rank = tensor_shape.Get().dim();
auto dim_index = GetCanonicalDimensionIndex(dim, rank);
return tensor_shape.Get().size(dim_index);
}
at::ScalarType LazyTensor::dtype() const {
return shape().Get().scalar_type();
}
MaybeRef<Shape> LazyTensor::shape() const {
if (data()->handle != nullptr) {
return Shape(data()->handle->shape());
}
if (data()->ir_value) {
// TODO(whc) remove shape from LazyTensor API too!
return data()->ir_value.shape();
}
auto const& tensor_data = data()->tensor_data;
TORCH_CHECK(tensor_data);
return Shape(tensor_data->scalar_type(), ToI64Vector(tensor_data->sizes()));
}
const BackendDevice& LazyTensor::GetDevice() const {
return data()->device;
}
int64_t LazyTensor::GetUniqueId() const {
return data()->unique_id;
}
BackendDataPtr LazyTensor::GetDataHandle() {
BackendDataPtr handle = CurrentDataHandle();
if (handle != nullptr) {
TORCH_CHECK(
handle->HasValue(),
"Trying to access data while an async operation is in flight: ",
handle->shape().to_string());
return handle;
}
if (data()->ir_value) {
ApplyPendingGraph();
} else {
auto const& tensor_data = data()->tensor_data;
TORCH_CHECK(tensor_data.has_value());
data()->handle = TensorToDataHandle(*tensor_data, GetDevice());
}
return data()->handle;
}
BackendDataPtr LazyTensor::CurrentDataHandle() const {
return data()->handle;
}
void LazyTensor::SetDataHandle(BackendDataPtr handle) {
SetDataHandle(std::move(handle), /*sync=*/true);
}
void LazyTensor::SetDataHandle(BackendDataPtr handle, bool sync) {
data()->handle = std::move(handle);
// Assigning a device data should always clear the IR node, to allow graph
// trimming.
AssignIrValue(Value());
if (sync) {
data()->tensor_data = std::nullopt;
}
}
void LazyTensor::SetIrValue(Value ir_value) {
data()->handle = nullptr;
data()->tensor_data = std::nullopt;
AssignIrValue(std::move(ir_value));
TryLimitGraphSize();
}
void LazyTensor::SetInPlaceIrValue(Value ir_value) {
auto tensor_shape = shape();
if (tensor_shape.Get().scalar_type() != ir_value.shape().scalar_type()) {
ir_value =
MakeCast(ir_value, tensor_shape.Get().scalar_type(), std::nullopt);
}
SetIrValue(std::move(ir_value));
}
void LazyTensor::AssignIrValue(Value ir_value) const {
data()->ir_value = std::move(ir_value);
data()->generation += 1;
}
void LazyTensor::TryLimitGraphSize() {
if (data()->ir_value &&
LazyGraphExecutor::Get()->IncTrimCounter() %
FLAGS_torch_lazy_trim_graph_check_frequency ==
0) {
size_t graph_size = Util::GetGraphSize({data()->ir_value.node.get()});
if (static_cast<int64_t>(graph_size) > FLAGS_torch_lazy_trim_graph_size) {
TORCH_LAZY_COUNTER("TrimIrGraph", 1);
ApplyPendingGraph();
}
}
}
Value LazyTensor::GetIrValue() const {
Value ir_value = CurrentIrValue();
if (ir_value) {
return ir_value;
}
BackendDataPtr handle = CurrentDataHandle();
if (handle != nullptr) {
// In case of tensor node, we do not clear the data when we set the IR
// node. This because we want further calls to GetIrValue() to fetch the
// same IR node, and not create new ones (even though the lowering context
// will still collapse them all into a single parameter op). So the call
// which wants the data will still find it, w/out having to fetch it via
// a computation client from-server call.
AssignIrValue(CreateTensorNode(handle, /*read_only=*/false));
return data()->ir_value;
}
std::optional<at::Tensor> tensor_data = CurrentTensorData();
TORCH_CHECK(tensor_data);
AssignIrValue(GetIrValueForTensor(*tensor_data, GetDevice()));
return data()->ir_value;
}
Value LazyTensor::CurrentIrValue() const {
return data()->ir_value;
}
void LazyTensor::SetTensorData(at::Tensor tensor_data) {
data()->tensor_data = std::move(tensor_data);
}
std::optional<at::Tensor> LazyTensor::CurrentTensorData() const {
return data()->tensor_data;
}
Value LazyTensor::GetIrValueForTensor(
const at::Tensor& tensor,
const BackendDevice& device) const {
BackendDataPtr data;
bool read_only = false;
if (tensor.dim() == 0 && tensor.numel() == 1) {
at::Scalar value = tensor.item();
if (IsSpecialScalar(value)) {
return MakeScalar(value, tensor.scalar_type());
}
data = LazyGraphExecutor::Get()->GetDeviceData(tensor.cpu(), device);
read_only = true;
} else {
TORCH_LAZY_TIMED("IrValueTensorToDataHandle");
data = TensorToDataHandle(tensor, device);
}
return CreateTensorNode(data, read_only);
}
at::Tensor LazyTensor::ToTensor(bool detached) {
at::Tensor tensor;
std::optional<at::Tensor> tensor_data = CurrentTensorData();
if (!tensor_data) {
LazyGraphExecutor::Get()->DeviceBarrier(GetDevice());
// The GetDataHandle() call will trigger an ApplyPendingGraph() if an IR
// Node is available on the tensor.
std::vector<at::Tensor> tensors =
DataHandlesToTensors({GetDataHandle()}, dtype());
tensor = std::move(tensors.front());
if (!detached) {
SetTensorData(tensor);
}
} else {
tensor = *tensor_data;
if (detached) {
if (data()->ir_value || data()->handle != nullptr) {
// If we have other authoritive sources, just drop our reference and
// transfer it to the caller.
data()->tensor_data = std::nullopt;
} else {
// Otherwise we need to make a copy to prevent the caller changing our
// version.
tensor = CopyTensor(tensor);
}
}
}
return tensor;
}
void LazyTensor::ShallowCopyTo(const LazyTensorPtr& dest) const {
dest->SetIrValue(GetIrValue());
}
void LazyTensor::SetTensor(at::Tensor tensor) {
SetTensorData(std::move(tensor));
data()->handle = nullptr;
AssignIrValue(Value());
}
void LazyTensor::UpdateFromTensor(const at::Tensor& tensor, bool sync) {
if (sync) {
at::Tensor typed_tensor = CopyTensor(tensor, dtype(), /*copy=*/false);
SetIrValue(GetIrValueForTensor(typed_tensor, GetDevice()));
} else {
SetTensorData(tensor);
data()->handle = nullptr;
AssignIrValue(Value());
}
}
void LazyTensor::UpdateFromTensorOut(const at::Tensor& tensor) {
UpdateFromTensor(tensor, /*sync=*/false);
}
void LazyTensor::UpdateFromTensorOut(const LazyTensorPtr& tensor) {
SetIrValue(tensor->GetIrValue());
}
Value LazyTensor::CreateTensorNode(const BackendDataPtr& data, bool read_only)
const {
data->SetInfo(std::make_shared<LazyGraphExecutor::DeviceDataInfo>(
GetUniqueId(), read_only));
return MakeDeviceData(data);
}
std::vector<LazyTensorPtr> LazyTensor::MakeOutputTensors(
const NodePtr& node) const {
std::vector<LazyTensorPtr> tensors;
tensors.reserve(node->num_outputs());
for (const auto i : c10::irange(node->num_outputs())) {
tensors.push_back(Create(Value(node, i), GetDevice()));
}
return tensors;
}
LazyTensorPtr LazyTensor::CopyTensorToDevice(const BackendDevice& device) {
// TODO: This can be optimized.
return Create(ToTensor(/*detached=*/true), device);
}
void LazyTensor::ApplyPendingGraph() {
LazyGraphExecutor::Get()->DeviceBarrier(GetDevice());
// This method is called to ensure that the tensor data is available on
// device, so that a call to CurrentDataHandle() returns a valid pointer.
if (CurrentDataHandle() == nullptr) {
std::vector<LazyTensorPtr> tensors(
{c10::make_intrusive<LazyTensor>(LazyTensor(*this))});
LazyGraphExecutor::Get()->SyncTensorsGraph(
&tensors,
{},
/*wait=*/true,
/*sync_ltc_data=*/false);
}
}
int64_t LazyTensor::GetNextTensorId() {
static std::atomic<int64_t>* id_generator = new std::atomic<int64_t>(1);
return id_generator->fetch_add(1);
}
torch::lazy::Value GetTensorList(at::ITensorListRef tensors) {
std::vector<Value> values;
for (const auto& t : tensors) {
auto* impl = dynamic_cast<LTCTensorImpl*>(t.unsafeGetTensorImpl());
TORCH_INTERNAL_ASSERT(
impl,
"GetTensorList only supports lists of valid tensors, but optional support could be added");
values.push_back(impl->tensor()->GetIrValue());
}
return torch::lazy::Value(torch::lazy::MakeTensorList(values));
}
LazyTensorPtr TryGetLtcTensor(const at::Tensor& tensor) {
auto* impl = dynamic_cast<LTCTensorImpl*>(
maybe_unwrap_functional(tensor).unsafeGetTensorImpl());
if (impl == nullptr) {
// return c10::make_intrusive<LazyTensor>();
return LazyTensorPtr();
}
return impl->tensor();
}
LazyTensorPtr GetLtcTensor(const at::Tensor& tensor) {
auto lazy_tensor = TryGetLtcTensor(tensor);
TORCH_CHECK(
lazy_tensor, "Input tensor is not a lazy tensor: ", tensor.toString());
return lazy_tensor;
}
std::vector<LazyTensorPtr> GetLtcTensors(c10::ArrayRef<at::Tensor> tensors) {
std::vector<LazyTensorPtr> ltc_tensors;
ltc_tensors.reserve(tensors.size());
for (const auto& tensor : tensors) {
ltc_tensors.emplace_back(TryGetLtcTensor(tensor));
}
return ltc_tensors;
}
LazyTensorPtr GetOrCreateLtcTensor(
const std::optional<at::Tensor>& tensor,
const BackendDevice& device) {
return GetOrCreateLtcTensor(tensor.value_or(at::Tensor()), device);
}
LazyTensorPtr GetLtcTensorOrCreateForWrappedNumber(
const at::Tensor& tensor,
const BackendDevice& device) {
// TODO: There are places in core where a scalar is wrapped but not marked as
// wrapped.
return (tensor.unsafeGetTensorImpl()->is_wrapped_number() ||
(tensor.dim() == 0 && tensor.numel() == 1))
? GetOrCreateLtcTensor(tensor, device)
: GetLtcTensor(tensor);
}
at::Tensor CreateAtenFromLtcTensor(const LazyTensorPtr& ltc_tensor) {
return ltc_tensor ? at::Tensor(c10::make_intrusive<LTCTensorImpl>(ltc_tensor))
: at::Tensor();
}
at::Tensor CreateAtenFromLtcTensor(LazyTensor&& ltc_tensor) {
return at::Tensor(c10::make_intrusive<LTCTensorImpl>(std::move(ltc_tensor)));
}
at::Tensor to_lazy_tensor(
const at::Tensor& self,
const c10::TensorOptions& options,
at::Device device,
bool non_blocking,
bool functionalize_output) {
TORCH_INTERNAL_ASSERT(self.device().type() != c10::kLazy);
TORCH_INTERNAL_ASSERT(device.type() == c10::kLazy);
auto eager_tensor =
self.to(options, /*non_blocking=*/non_blocking, /*copy=*/true);
auto lazy_self = torch::lazy::GetOrCreateLtcTensor(
eager_tensor, torch::lazy::atenDeviceToBackendDevice(device));
auto out = torch::lazy::CreateAtenFromLtcTensor(lazy_self);
if (functionalize_output) {
// See Note [Lazy Tensor Functionalization]
return at::functionalization::impl::to_functional_tensor(out);
} else {
return out;
}
}
} // namespace torch::lazy
|