Skip to content

Commit

Permalink
Merge branch 'master' into remove-is
Browse files Browse the repository at this point in the history
  • Loading branch information
lin-hitonami authored May 6, 2023
2 parents 43cfaf8 + 0599ecc commit d066572
Show file tree
Hide file tree
Showing 34 changed files with 380 additions and 283 deletions.
19 changes: 17 additions & 2 deletions .github/workflows/scripts/ti_build/alter.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ def add_aot_env():

def _write_ti_bashrc():
path = get_cache_home() / "ti.bashrc"
envs = get_cache_home() / "ti-env.sh"
_write_env(envs)
with open(path, "w") as f:
f.write(
"[ -f /etc/bashrc ] && source /etc/bashrc\n"
"[ -f ~/.bashrc ] && source ~/.bashrc\n"
r'export PS1="\[\e]0;[Taichi Build Environment]\a\]\[\033[01;31m\][Taichi Build] \[\033[00m\]$PS1"'
"\n"
f"source {envs}\n"
)

return path
Expand All @@ -47,11 +51,15 @@ def _write_ti_zshrc():
dotdir = get_cache_home() / "zdotdir"
dotdir.mkdir(parents=True, exist_ok=True)
path = dotdir / ".zshrc"
envs = get_cache_home() / "ti-env.sh"
_write_env(envs)
with open(path, "w") as f:
f.write(
"[ -f /etc/zsh/zshrc ] && source /etc/zsh/zshrc\n"
"[ -f $HOME/.zshrc ] && source $HOME/.zshrc\n"
r"export PROMPT='%{$fg_bold[red]%}[Taichi Build] %{$reset_color%}'$PROMPT"
"\n"
f"source {envs}\n"
)
return dotdir

Expand Down Expand Up @@ -138,10 +146,13 @@ def enter_shell():
os.execl(shell.exe, shell.exe)


def write_env(path):
cmake_args.writeback()
def _write_env(path):
envs = os.environ.get_changed_envs()
envstr = ""

if isinstance(path, Path):
path = str(path)

if path.endswith(".ps1"):
envstr = "\n".join([f'$env:{k}="{v}"' for k, v in envs.items()])
elif path.endswith(".sh"):
Expand All @@ -156,6 +167,10 @@ def write_env(path):
with open(path, "w") as f:
f.write(envstr)


def write_env(path):
cmake_args.writeback()
_write_env(path)
misc.info(f"Environment variables written to {path}")


Expand Down
19 changes: 19 additions & 0 deletions c_api/docs/taichi/taichi_core.h.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ Types of kernel and compute graph argument.
- `enumeration.argument_type.ndarray`: ND-array wrapped around a `handle.memory`.
- `enumeration.argument_type.texture`: Texture wrapped around a `handle.image`.
- `enumeration.argument_type.scalar`: Typed scalar.
- `enumeration.argument_type.tensor`: Typed tensor.
`bit_field.memory_usage`
Expand Down Expand Up @@ -450,6 +451,23 @@ Scalar value represented by a power-of-two number of bits.
A typed scalar value.
`union.tensor_value`
Tensor value represented by a power-of-two number of bits.
- `union.tensor_value.x8`: Tensor value that fits into 8 bits.
- `union.tensor_value.x16`: Tensor value that fits into 16 bits.
- `union.tensor_value.x32`: Tensor value that fits into 32 bits.
- `union.tensor_value.x64`: Tensor value that fits into 64 bits.
`structure.tensor_value_with_length`
A tensor value with a length.
`structure.tensor`
A typed tensor value.
`union.argument_value`
A scalar or structured argument value.
Expand All @@ -459,6 +477,7 @@ A scalar or structured argument value.
- `union.argument_value.ndarray`: An ND-array to be bound.
- `union.argument_value.texture`: A texture to be bound.
- `union.argument_value.scalar`: An scalar to be bound.
- `union.argument_value.tensor`: A tensor to be bound.
`structure.argument`
Expand Down
30 changes: 25 additions & 5 deletions c_api/include/taichi/cpp/taichi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,25 @@ class ComputeGraph {
return compute_graph_;
}
};
template <typename T>
struct DataTypeToEnum {
static constexpr TiDataType value = TI_DATA_TYPE_UNKNOWN;
};
#define DEFINE_DATA_TYPE_ENUM(type, enumv) \
template <> \
struct DataTypeToEnum<type> { \
static constexpr TiDataType value = TI_DATA_TYPE_##enumv; \
};

DEFINE_DATA_TYPE_ENUM(int32_t, I32);
DEFINE_DATA_TYPE_ENUM(float, F32);
DEFINE_DATA_TYPE_ENUM(uint16_t, U16);
DEFINE_DATA_TYPE_ENUM(int16_t, I16);
DEFINE_DATA_TYPE_ENUM(uint8_t, U8);
DEFINE_DATA_TYPE_ENUM(int8_t, I8);
DEFINE_DATA_TYPE_ENUM(uint64_t, U64);
DEFINE_DATA_TYPE_ENUM(int64_t, I64);
#undef DEFINE_DATA_TYPE_ENUM

class Kernel {
protected:
Expand Down Expand Up @@ -884,11 +903,12 @@ class Kernel {
template <typename T>
void push_arg(const std::vector<T> &v) {
int idx = args_.size();
// Temporary workaround for setting vec/matrix arguments in a flattened way.
args_.resize(args_.size() + v.size());
for (int j = 0; j < v.size(); ++j) {
at(idx + j) = v[j];
}
args_.resize(idx + 1);
args_[idx].type = TI_ARGUMENT_TYPE_TENSOR;
std::memcpy(args_[idx].value.tensor.contents.data.x32, v.data(),
v.size() * sizeof(T));
args_[idx].value.tensor.contents.length = v.size();
args_[idx].value.tensor.type = DataTypeToEnum<T>::value;
}

template <typename T>
Expand Down
36 changes: 35 additions & 1 deletion c_api/include/taichi/taichi_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@
#pragma once

#ifndef TI_C_API_VERSION
#define TI_C_API_VERSION 1005000
#define TI_C_API_VERSION 1007000
#endif // TI_C_API_VERSION

#ifndef TAICHI_H
Expand Down Expand Up @@ -463,6 +463,8 @@ typedef enum TiArgumentType {
TI_ARGUMENT_TYPE_TEXTURE = 3,
// Typed scalar.
TI_ARGUMENT_TYPE_SCALAR = 4,
// Typed tensor.
TI_ARGUMENT_TYPE_TENSOR = 5,
TI_ARGUMENT_TYPE_MAX_ENUM = 0xffffffff,
} TiArgumentType;

Expand Down Expand Up @@ -802,6 +804,36 @@ typedef struct TiScalar {
TiScalarValue value;
} TiScalar;

// Union `TiTensorValue`
//
// Tensor value represented by a power-of-two number of bits.
typedef union TiTensorValue {
// Tensor value that fits into 8 bits.
uint8_t x8[128];
// Tensor value that fits into 16 bits.
uint16_t x16[64];
// Tensor value that fits into 32 bits.
uint32_t x32[32];
// Tensor value that fits into 64 bits.
uint64_t x64[16];
} TiTensorValue;

// Structure `TiTensorValueWithLength`
//
// A tensor value with a length.
typedef struct TiTensorValueWithLength {
uint32_t length;
TiTensorValue data;
} TiTensorValueWithLength;

// Structure `TiTensor`
//
// A typed tensor value.
typedef struct TiTensor {
TiDataType type;
TiTensorValueWithLength contents;
} TiTensor;

// Union `TiArgumentValue` (1.4.0)
//
// A scalar or structured argument value.
Expand All @@ -818,6 +850,8 @@ typedef union TiArgumentValue {
TiTexture texture;
// An scalar to be bound.
TiScalar scalar;
// A tensor to be bound.
TiTensor tensor;
} TiArgumentValue;

// Structure `TiArgument` (1.4.0)
Expand Down
22 changes: 22 additions & 0 deletions c_api/src/taichi_core_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,28 @@ void ti_launch_kernel(TiRuntime runtime,
devallocs.emplace_back(std::move(devalloc));
break;
}
case TI_ARGUMENT_TYPE_TENSOR: {
auto &tensor = arg.value.tensor;
if (tensor.type == TI_DATA_TYPE_I16 ||
tensor.type == TI_DATA_TYPE_U16 ||
tensor.type == TI_DATA_TYPE_F16) {
for (int j = 0; j < tensor.contents.length; j++) {
builder.set_struct_arg_impl({(int)i, j},
tensor.contents.data.x16[j]);
}
} else if (tensor.type == TI_DATA_TYPE_I32 ||
tensor.type == TI_DATA_TYPE_U32 ||
tensor.type == TI_DATA_TYPE_F32) {
for (int j = 0; j < tensor.contents.length; j++) {
builder.set_struct_arg_impl({(int)i, j},
tensor.contents.data.x32[j]);
}
} else {
ti_set_last_error(TI_ERROR_NOT_SUPPORTED,
("args[" + std::to_string(i) + "].type").c_str());
}
break;
}
default: {
ti_set_last_error(TI_ERROR_ARGUMENT_OUT_OF_RANGE,
("args[" + std::to_string(i) + "].type").c_str());
Expand Down
61 changes: 60 additions & 1 deletion c_api/taichi.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@
"f32": 1,
"ndarray": 2,
"texture": 3,
"scalar": 4
"scalar": 4,
"tensor": 5
}
},
{
Expand Down Expand Up @@ -484,6 +485,60 @@
}
]
},
{
"name": "tensor_value",
"type": "union",
"variants": [
{
"name": "x8",
"type": "uint8_t",
"count": 128
},
{
"name": "x16",
"type": "uint16_t",
"count": 64
},
{
"name": "x32",
"type": "uint32_t",
"count": 32
},
{
"name": "x64",
"type": "uint64_t",
"count": 16
}
]
},
{
"name": "tensor_value_with_length",
"type": "structure",
"fields": [
{
"name": "length",
"type": "uint32_t"
},
{
"name": "data",
"type": "union.tensor_value"
}
]
},
{
"name": "tensor",
"type": "structure",
"fields": [
{
"name": "type",
"type": "enumeration.data_type"
},
{
"name": "contents",
"type": "structure.tensor_value_with_length"
}
]
},
{
"name": "argument_value",
"type": "union",
Expand All @@ -508,6 +563,10 @@
{
"name": "scalar",
"type": "structure.scalar"
},
{
"name": "tensor",
"type": "structure.tensor"
}
]
},
Expand Down
4 changes: 4 additions & 0 deletions docs/lang/articles/c-api/taichi_core.md
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ typedef enum TiArgumentType {
TI_ARGUMENT_TYPE_NDARRAY = 2,
TI_ARGUMENT_TYPE_TEXTURE = 3,
TI_ARGUMENT_TYPE_SCALAR = 4,
TI_ARGUMENT_TYPE_TENSOR = 5,
TI_ARGUMENT_TYPE_MAX_ENUM = 0xffffffff,
} TiArgumentType;
```
Expand All @@ -509,6 +510,7 @@ Types of kernel and compute graph argument.
- `TI_ARGUMENT_TYPE_NDARRAY`: ND-array wrapped around a [`TiMemory`](#handle-timemory).
- `TI_ARGUMENT_TYPE_TEXTURE`: Texture wrapped around a [`TiImage`](#handle-tiimage).
- `TI_ARGUMENT_TYPE_SCALAR`: Typed scalar.
- `TI_ARGUMENT_TYPE_TENSOR`: Typed tensor.


---
Expand Down Expand Up @@ -927,6 +929,7 @@ typedef union TiArgumentValue {
TiNdArray ndarray;
TiTexture texture;
TiScalar scalar;
TiTensor tensor;
} TiArgumentValue;
```

Expand All @@ -937,6 +940,7 @@ A scalar or structured argument value.
- `ndarray`: An ND-array to be bound.
- `texture`: A texture to be bound.
- `scalar`: An scalar to be bound.
- `tensor`: A tensor to be bound.

---
### Structure `TiArgument`
Expand Down
Loading

0 comments on commit d066572

Please sign in to comment.