对这个问题的一般解决方案正在在工作这个 github上的问题,但如果有使用的解决方法我想知道tf.gather(或别的东西)采用多指标达到数组索引。我想出的一个解决方案是广播将多idx中的每个索引乘以张量形状的累加乘积,从而生成适合于索引平坦张量的索引:
import tensorflow as tf
import numpy as np
def __cumprod(l):
# Get the length and make a copy
ll = len(l)
l = [v for v in l]
# Reverse cumulative product
for i in range(ll-1):
l[ll-i-2] *= l[ll-i-1]
return l
def ravel_multi_index(tensor, multi_idx):
"""
Returns a tensor suitable for use as the index
on a gather operation on argument tensor.
"""
if not isinstance(tensor, (tf.Variable, tf.Tensor)):
raise TypeError('tensor should be a tf.Variable')
if not …Run Code Online (Sandbox Code Playgroud) 我来自C++背景,并想知道我是否可以编写一个特性用于foo和bar函数:
#![feature(alloc)]
use std::rc::{Rc, Weak};
pub trait MyTrait {
/// Value type
type VAL;
/// Strongly boxed type
/// Will be constrained to something like Box, Rc, Arc
type SB;
/// Weakly boxed type
type WB;
}
struct MyFoo;
impl MyTrait for MyFoo {
type VAL = i64;
type SB = Rc<i64>;
type WB = Weak<i64>;
}
fn foo<T: MyTrait>(value: T::VAL) {}
// Uncomment
// fn bar<T: MyTrait>(rc_val: T::SB<T::VAL>) {}
fn main() {
let …Run Code Online (Sandbox Code Playgroud) 在张量流中我注册一个操作,如下所示:
REGISTER_OP("RimeBSqrt")
.Input("stokes: FT")
.Input("alpha: FT")
.Input("frequency: FT")
.Input("ref_freq: FT")
.Output("b_sqrt: CT")
.Attr("FT: {float, double} = DT_FLOAT")
.Attr("CT: {complex64, complex128} = DT_COMPLEX64");
Run Code Online (Sandbox Code Playgroud)
上述所有输入都是张量,但 ref_freq 是标量或 0 维张量。在 CPU 内核的 Compute() 方法中,我可以执行以下操作来提取标量:
const Tensor & in_ref_freq = context->input(3);
FT ref_freq = in_ref_freq.tensor<FT, 1>()(0);
Run Code Online (Sandbox Code Playgroud)
然而,相同类型的代码会在我的 GPU 内核的 Compute() 方法中生成段错误,因为 CPU 现在尝试访问 GPU 设备上的内存块。在将这个标量值发送到 GPU 之前是否有办法拦截它?我想避免 CUDA 内核中出现以下额外级别的内存间接寻址:
template <typename FT>
__global__ void kernel(..., FT * ref_freq, ...)
{
FT value = ref_freq[0];
}
Run Code Online (Sandbox Code Playgroud)
我不认为Attr这是使用的方法,ref_freq因为它是可变的、可配置的值。
我想使用 CUDA 9.0 的 shuffle 操作实现基本的阻塞加载和扭曲转置。我知道 cub 和 trove 实现,但我仅限于使用 nvrtc 进行编译,并且标准头文件包含使这些库难以满足。我不是在寻找任何花哨的东西,只是对维度为 2 的幂的数据进行一些整数、浮点和双洗牌。
可视化经线大小为 8 的示例,我想从:
correlation
0 1 2 3
lane 0 0 8 16 24
lane 1 1 9 17 25
lane 2 2 10 18 26
lane 3 3 11 19 27
lane 4 4 12 20 28
lane 5 5 13 21 29
lane 6 6 14 22 30
lane 7 7 15 23 31
Run Code Online (Sandbox Code Playgroud)
到这个结构:
correlation
0 1 2 3
lane 0 0 1 …Run Code Online (Sandbox Code Playgroud)