텐서 연산¶
생성¶
zeros¶
1 | |
Create a tensor with zeros.
Parameters:
tensor_type(Value): The type of the tensor.dynamic_shape(Value | None): The dynamic shape of the tensor. If none, tensor type is assumed to have complete shape.
ones¶
1 | |
Create a tensor filled with ones.
Parameters:
tensor_type(Value): The type of the tensor.dynamic_shape(Optional[Value]): The dynamic shape of the tensor. If none, tensor type is assumed to have complete shape.
full¶
1 | |
Create a tensor filled with a scalar value.
Parameters:
tensor_type(Value): The type of the tensor.fill_value(Value): The scalar value to fill. Must have the same dtype as the tensor element type. Usearith.castto convert if needed.dynamic_shape(Optional[Value]): The dynamic shape of the tensor. If none, tensor type is assumed to have complete shape. Default:None.
full_like¶
1 | |
Create a tensor filled with a scalar value, using the input tensor's shape.
Parameters:
tensor(Value): The reference tensor whose shape and placement are used.fill_value(Value): The scalar value to fill. Must have the same dtype as the tensor's element type. Usearith.castto convert if needed.
이동¶
copy¶
1 | |
Copy the values from the source value to target, preserving the lifetime of source value.
Note:
This operation will verify if copy from assigned device of value to device is possible according to the compute model.
Parameters:
value(Value): The value to copytarget(Value): The device to copy to. If None, the value is copied to the default device
Returns:
result(Value): The copied value.
Example:
1 2 3 4 5 6 7 | |
memcpy¶
1 | |
Copy the values from the source value to host, preserving the lifetime of source value.
Note:
This operation will verify if copy operation from value to dst is possible according to the compute model.
Parameters:
dst(Value): The device to copy to.src(Value): The value to copy.
Returns:
result(Value): The copied value.
Example:
1 2 3 4 5 6 | |
move¶
1 | |
Move values from the source value to target, invalidating the lifetime of source value.
Move operation removes the lifetime of the source value, and transfers it to the returned value.
Key Characteristics:
- Ownership transfer - Source tensor becomes invalid after move
- Zero-copy optimization - When possible, avoids actual data copying
- Compile-time validation - Compiler verifies move validity using compute model
- Memory efficiency - Prevents accidental use of moved tensor
Note:
This operation will verify if move from assigned device of value to target_device is possible according to the compute model.
Parameters:
value(Value): The value to move. Must be an interpreted value at invocation time.target_device(Value): Device or memory to move to (as an interpreted value representing anExecutororMemory).
Returns:
result(Value): The moved value whose lifetime replaces the source value's lifetime.
Example:
1 2 3 4 5 6 | |
Warning:
Moving a tensor to a different device will invalidate the lifetime of the source tensor. Therefore, the source tensor cannot be used after the move operation. And trying to use the source tensor will cause a compiler error.
1 2 3 4 | |
request_load¶
1 | |
Request a copy of the values from the source value to target, preserving the lifetime of source value.
Returns a handle, which is ID of the request, which will be consumed by receive operation.
Key Features:
- Asynchronous operation - Data loading happens in the background
- Handle-based access - Use
legato.tensor.receiveto consume the handle and access data - Automatic cleanup - Compiler warns and automatically deletes unused handles (when statically analyzable)
- Memory efficiency - Enables overlapping of computation and data movement
Parameters:
tensor(Value): The compiled value to request a copy of.devices(Value): Target device(s) to load to. Accepts a single device or a list of devices. Each device is an interpretedExecutororMemoryvalue.target_name(Value): Target name to receive the tensor. (interpreted value)
Note:
Bertha uses static receive slots; do not forward this SSA handle to receive_type on Bertha. Match explicit target names on load and receive.
Returns:
result(Value): A handle (request ID) to be consumed byreceive.
Example:
1 2 3 4 5 6 7 8 9 | |
Receive¶
receive¶
1 | |
Receive the values from the source value to target, consuming the handle.
The handle is returned by request_copy or request_move operation.
Key Features:
- Handle consumption - The handle is consumed and cannot be used again
- Context requirement - Must be called within the appropriate compute context
- Synchronous operation - Blocks until data is available
- Type safety - Compiler ensures handle validity and type consistency
Note:
Handle consumption will not work on Bertha, since it does not have hardware support for handle consumption.
Parameters:
tensor(Value): Target tensor to receive. (compiled value)handle(Value): Request handle returned byrequest_load. (compiled or interpreted value)target_name(Value): Target name to receive the tensor. (interpreted value)
Returns:
result(Value): The received value materialized with the requested type.
Example:
1 2 3 4 5 6 7 8 9 | |
receive_type¶
1 | |
Receive the values from the source value to target, consuming the handle.
The handle is returned by request_copy or request_move operation.
Parameters:
tensor_type(Value): Target tensor type to receive. (compiled value)handle(Value): Request handle returned byrequest_load. (compiled value)dynamic_shape(Value): Dynamic shape of the tensor. (compiled value)target_name(Value): Target name to receive the tensor. (interpreted value)
Returns:
result(Value): The received value materialized with the requested type.
View와 shape¶
view¶
1 | |
Creates view of the value. This will borrow lifetime from the original value.
Returned value will share the same memory with the original value. This operation is no-op from the hardware, but provides a simplified interface for indexing tensors.
Example:
1 2 3 4 5 | |
Parameters:
value(Value): The compiled value (tensor) to view.indices(Value): Compiled tuple of index, or range representing the view. Length of the tuple must match the rank of the input tensor. Dimensions with index is collapsed. Therefore, tuple must contain at least single range entry.
Returns:
result(Value): A view value that aliases the storage ofvalue.
Example:
1 2 3 4 5 6 7 | |
reshape¶
1 | |
Reshape the value to the new shape.
This operation is no-op from the hardware, but compiler can use this to retag a tensor with a new shape/rank after target-specific validation.
Parameters:
value(Value): The compiled value to reshape.shape(Value): New shape, using the same syntax as tensor type shapes. A scalar dimension is interpreted as(logical, physical) == (d, d). A tuple dimension is interpreted as(logical, physical). Use-1for a dynamic logical dimension.dynamic_shape(Value | None): Optional tuple of runtime values for dynamic logical dimensions inshape. Default:None.
Returns:
result(Value): The reshaped value with the specified shape.
Example:
1 2 3 4 5 6 | |
Shape 제약
reshape한 텐서의 물리 원소 총수는 입력 텐서의 물리 원소 총수와 같아야 합니다. 또한 새 논리 shape이 유효한 데이터 범위를 포함하는지는 이 연산에서 검증하지 않습니다. 논리 shape을 올바르게 지정했는지는 프로그래머가 직접 확인해야 합니다.
Masking¶
다음은 프론트엔드 API입니다.
standalone full, full_like, zeros, ones fill은 EVT0 바이너리에서 일반적으로 쓸 수 있는 연산이 아니며,
다만 제한된 형태의 fill은 지원되는 fusion이 소비할 수 있습니다.
프론트엔드 전용임을 명시한 예제는 텐서 메모리 패턴을,
백엔드 지원 범위는 Math 연산의 백엔드 지원을 참고하십시오.
standalone fill이 EVT0 assignment 단계까지 남아 있으면 headline LE0121이 보고되고,
기록된 사유로 LE2104가 함께 표시됩니다.
실제 컴파일 오류를 확인하는 예제는
컴파일 diagnostics를 참고하십시오.
where¶
1 | |
Select values element-wise based on a boolean condition.
Parameters:
condition(Value): Boolean tensor or scalar condition.true_value(Value): Values selected where condition is true. Must have exactly the same type asfalse_value.false_value(Value): Values selected where condition is false. Must have exactly the same type astrue_value. Uselegato.tensor.full_liketo materialize scalar fallback values for tensor branches.
Shape 제약
condition, true_value, false_value는 모두 shape이 정확히 같아야 합니다.
condition은 bool 타입이어야 하고, true_value / false_value는 dtype이 정확히 같아야 합니다.
broadcasting은 지원하지 않습니다.
scalar 대체 값은 where에 넘기기 전에
legato.tensor.full_like로 필요한 텐서 shape에 맞게 확장하십시오.
tril¶
1 | |
Return the lower triangular part of a tensor.
Parameters:
tensor(Value): The input tensor (rank >= 2).diagonal(Value): The diagonal offset. 0 means the main diagonal.
Rank 요구 사항
입력 텐서는 rank가 2 이상이어야 합니다. 이 연산은 마지막 두 차원의 하삼각 부분을 추출합니다.
조회¶
get_rank¶
1 | |
Get the rank of the value.
Parameters:
value(Value): The compiled value to get the rank of.
Returns:
result(Value): The rank of the value.
get_dimension¶
1 | |
Get the dimension of the value at the given index.
Parameters:
value(Value): The compiled value to get the dimension of.index(Value): The index of the dimension to get. (index or integer value)
Returns:
result(Value): The dimension of the value at the given index.
load와 receive 연산에서는 타겟 이름을 명시적으로 지정합니다. 현재 호출 형태는 다음과 같습니다.
1 2 3 | |
수신할 타입과 shape을 나타내는 텐서 값이 이미 있으면 legato.tensor.receive를 사용하십시오.
타입과 선택 사항인 동적 shape만 있으면 legato.tensor.receive_type을 사용하십시오.
receive_type은 핸들을 사용해 그 타입의 텐서를 실체화(materialize)합니다.
예:
1 2 3 4 5 | |
튜플 원소 빌리기¶
1 | |
borrow_tuple_element는 튜플에 들어 있는 텐서 원소 하나를 저장소를 복사하지 않고 선택합니다.
인덱스는 컴파일 시점에 정해지는 음이 아닌 정수이며, 튜플 범위 안에 있어야 합니다.
결과는 선택한 원소의 alias이며, 빌린 결과가 살아 있는 동안에는 원소를 만든 쪽의 저장소도 함께 유지됩니다.
두 값이 저장소를 공유하므로, 빌린 텐서에 값을 쓰면 원래 튜플 원소도 함께 바뀝니다. 쓴 내용이 원본에 그대로 반영되는 alias를 의도한 경우가 아니라면, 이 연산은 읽기 전용 consumer에만 사용하십시오. 일반적인 튜플 인덱싱과 destructuring은 기존대로 소유권을 가진 값을 만들어 내며, 이 과정에서 텐서 원소가 복사될 수 있습니다.
원소를 빌리는 방식은 fused 연산에서 읽기 전용 결과를 꺼내는 경우처럼, 튜플을 만드는 연산과 그 consumer의 저장소 수명이 같다는 것이 확실할 때 사용하십시오. consumer에 독립된 저장소가 필요하면 일반적인 인덱싱을 사용하십시오.
참고:
- 핸들은 알맞은 컨텍스트와 타겟 버퍼에서 소비됩니다.
receive_type은 텐서 타입과 함께, index/정수/range 값으로 이루어진 동적 shape 튜플을 선택적으로 받습니다. shape이 완전히 정적이면 빈 튜플이나None을 넘기십시오.