2022-02-26 08:50:09 +08:00
|
|
|
//===- mlir_node.h --------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
// Also available under a BSD-style license. See LICENSE.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// This file is adapted from pytorch/pytorch
|
2022-04-14 03:42:02 +08:00
|
|
|
// https://github.com/pytorch/pytorch/blob/master/torch/csrc/lazy/ts_backend/ts_node.h
|
2022-02-26 08:50:09 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2022-02-18 09:15:37 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <ATen/core/interned_strings.h>
|
2022-04-14 03:42:02 +08:00
|
|
|
#include <torch/csrc/jit/ir/ir.h>
|
2022-02-18 09:15:37 +08:00
|
|
|
#include <torch/csrc/lazy/backend/lowering_context.h>
|
|
|
|
#include <torch/csrc/lazy/core/ir.h>
|
2022-03-24 22:15:43 +08:00
|
|
|
#include <torch/csrc/lazy/core/shape.h>
|
2022-02-18 09:15:37 +08:00
|
|
|
|
|
|
|
#include "mlir_lowering_context.h"
|
2022-07-13 03:56:52 +08:00
|
|
|
#include "utils/debug.h"
|
|
|
|
#include "utils/exception.h"
|
2022-02-18 09:15:37 +08:00
|
|
|
|
|
|
|
namespace torch {
|
|
|
|
namespace lazy {
|
|
|
|
|
2022-04-14 03:42:02 +08:00
|
|
|
class TORCH_API TorchMlirNode : public torch::lazy::Node {
|
2022-03-24 22:15:43 +08:00
|
|
|
public:
|
2024-01-30 01:59:33 +08:00
|
|
|
TorchMlirNode(OpKind op, OpList operands, std::vector<Shape> &&shapes,
|
|
|
|
size_t num_outputs, hash_t hash_seed = kHashSeed);
|
2022-05-03 21:35:44 +08:00
|
|
|
|
2024-01-30 01:59:33 +08:00
|
|
|
TorchMlirNode(OpKind op, OpList operands,
|
|
|
|
const std::function<Shape()> &shape_fn, size_t num_outputs,
|
|
|
|
hash_t hash_seed = kHashSeed);
|
2022-05-03 21:35:44 +08:00
|
|
|
|
2024-01-30 01:59:33 +08:00
|
|
|
TorchMlirNode(OpKind op, OpList operands, size_t num_outputs,
|
|
|
|
hash_t hash_seed = kHashSeed);
|
2022-05-03 21:35:44 +08:00
|
|
|
|
2024-01-30 01:59:33 +08:00
|
|
|
TorchMlirNode(OpKind op, Shape shape, size_t num_outputs,
|
|
|
|
hash_t hash_seed = kHashSeed);
|
2022-05-03 21:35:44 +08:00
|
|
|
|
2024-01-30 01:59:33 +08:00
|
|
|
// Adds a static hook that is run after every single TorchMlirNode is
|
|
|
|
// constructed
|
|
|
|
static void addConstructorHook(std::function<void(TorchMlirNode *)>);
|
2022-09-29 23:43:39 +08:00
|
|
|
|
2022-06-08 02:38:50 +08:00
|
|
|
~TorchMlirNode() override = default;
|
|
|
|
|
2022-05-03 21:35:44 +08:00
|
|
|
hash_t hash() const override;
|
|
|
|
|
|
|
|
hash_t shapeHash() const override;
|
2022-02-18 09:15:37 +08:00
|
|
|
|
2024-01-30 01:59:33 +08:00
|
|
|
TorchMlirNode *mlir_node(int index) const;
|
2022-09-29 23:43:39 +08:00
|
|
|
|
2024-01-30 01:59:33 +08:00
|
|
|
virtual TorchMlirOpVector Lower(TorchMlirFunction function,
|
|
|
|
TorchMlirLoweringContext *loctx) const;
|
2022-05-03 21:35:44 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
// The hash of the dag WITH size info. Used for shape caching
|
|
|
|
hash_t shape_hash_;
|
|
|
|
// The hash of the dag used to look up the compiled graph by a hash
|
|
|
|
// in this case, we will use the dag hash WITHOUT size info if dynamic shape
|
|
|
|
// is enabled and use the dag hash WITH size info otherwise.
|
|
|
|
hash_t dag_hash_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// TensorList represents an at::TensorList which is a vector[Tensor] but is also
|
|
|
|
// a first-class IValue and can be fed as a single input to a TS program. It is
|
|
|
|
// much easier to handle TensorLists in Lazy Tensor code if they are represented
|
|
|
|
// as a single Node so there can be more than one TensorList and more than one
|
|
|
|
// Tensor side-by-side as operands to an op.
|
|
|
|
//
|
|
|
|
// Note: shape is undefined for TensorList. We assert in some places that
|
|
|
|
// #shapes matches #outputs and this stems from
|
|
|
|
// the fact that currently all IR nodes represent tensors (there is no
|
2023-11-01 11:10:47 +08:00
|
|
|
// type system for this IR). Because of this, TensorList is a bit of a
|
2022-05-03 21:35:44 +08:00
|
|
|
// hack.
|
|
|
|
//
|
|
|
|
// TODO(whc) once Shape() API is moved to Node base, also make it virtual, and
|
|
|
|
// then implement it as NotImplemented for TensorList, also fixing the assertion
|
|
|
|
// that would fail.
|
2022-06-08 02:38:50 +08:00
|
|
|
struct TORCH_API TorchMlirTensorList : public TorchMlirNode {
|
|
|
|
static OpKind ClassOpKind();
|
|
|
|
|
|
|
|
TorchMlirTensorList() = delete;
|
|
|
|
TorchMlirTensorList(OpList values);
|
2022-05-03 21:35:44 +08:00
|
|
|
|
2024-01-30 01:59:33 +08:00
|
|
|
torch::lazy::TorchMlirOpVector
|
|
|
|
Lower(TorchMlirFunction function,
|
|
|
|
TorchMlirLoweringContext *loctx) const override;
|
2022-02-18 09:15:37 +08:00
|
|
|
};
|
|
|
|
|
2024-01-30 01:59:33 +08:00
|
|
|
// TorchMlirOptionalTensorList is similar to TorchMlirTensorList but it can also
|
|
|
|
// represent optional tensors, so the output type for this op is
|
|
|
|
// !torch.list<optional<vtensor>>.
|
2023-08-30 18:29:39 +08:00
|
|
|
struct TORCH_API TorchMlirOptionalTensorList : public TorchMlirNode {
|
|
|
|
static OpKind ClassOpKind();
|
|
|
|
|
|
|
|
TorchMlirOptionalTensorList() = delete;
|
|
|
|
TorchMlirOptionalTensorList(OpList values);
|
|
|
|
|
2024-01-30 01:59:33 +08:00
|
|
|
torch::lazy::TorchMlirOpVector
|
|
|
|
Lower(TorchMlirFunction function,
|
|
|
|
TorchMlirLoweringContext *loctx) const override;
|
2023-08-30 18:29:39 +08:00
|
|
|
};
|
|
|
|
|
2022-03-24 22:15:43 +08:00
|
|
|
} // namespace lazy
|
|
|
|
} // namespace torch
|