This code might look a bit hacky, but should be a bit faster than your current loop:
N, C, H = 64, 100, 32
data = torch.randn(N, C, H)
input_lens = torch.randint(2, H, (N,))

def fun1(data, input_lens):
    ret = torch.stack([
        torch.sum(data[i, :l, :], dim=0) / l for i, l in enumerate(input_lens)
    ])
    return ret

def fun2(data, input_lens):
    idx = torch.arange(C).unsqueeze(0).expand(N, -1)
    idx = idx < input_lens.unsqueeze(1)
    idx = idx.unsqueeze(2).expand(-1, -1, H)
    ret = (data * idx.float()).sum(1) / input_lens.unsqueeze(1).float()
    return ret

%timeit fun1(data, input_lens)
> 906 µs ± 5.91 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit fun2(data, input_lens)
> 388 µs ± 5.09 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

I’m not sure what your shapes are, so you might try profiling it for your use case.