Skip to content

[Code scan] Initialize MapFltNvnmd outputs and fix its input contract #5655

Description

@njzjz

This issue comes from a Codex global scan of deepmodeling/deepmd-kit at commit 73de44b1f94471b2e3bdb6b11f57b34d7bc791bb.

Problem

MapFltNvnmd has a stale input-count check and can leave output entries uninitialized for out-of-range inputs.

The op registration declares four inputs:

  • //- register the operator
    // prec = 2^n, so it doesn't need to match `T`
    REGISTER_OP("MapFltNvnmd")
    .Attr("T: {float, double} = DT_DOUBLE")
    .Input("x: T")
    .Input("table: T")
    .Input("table_grad: T")
    .Input("table_info: T")
    .Output("y: T");

Compute() still debug-checks for three inputs, but then reads input 3:

  • void Compute(OpKernelContext* context) override {
    DCHECK_EQ(3, context->num_inputs());
    const Tensor& t_x = context->input(0);
    const Tensor& t_table = context->input(1);
    const Tensor& t_table_info = context->input(3);

After allocating y, the loop skips any x value outside all table intervals:

  • //- 1.create tensor
    TensorShape shY;
    shY.AddDim(N);
    shY.AddDim(D);
    shY.AddDim(M);
    Tensor* t_y = NULL;
    //- 2.allocate the memory
    //* allocate memory for the Y tensor which is called output 0
    OP_REQUIRES_OK(context, context->allocate_output(0, shY, &t_y));
    auto x = t_x.flat<FPTYPE>().data();
    auto table = t_table.flat<FPTYPE>().data();
    auto info = t_table_info.flat<FPTYPE>().data();
    auto y = t_y->flat<FPTYPE>().data();
  • for (ss = S - 1; ss >= 0; ss--) {
    x0 = info[ss * 5 + 0];
    x1 = info[ss * 5 + 1];
    dx = info[ss * 5 + 2];
    N0 = int(info[ss * 5 + 3]);
    N1 = int(info[ss * 5 + 4]);
    dN = N1 - N0;
    for (ii = 0; ii < N * D; ii++) {
    // cal idx and xx
    xi = x[ii];
    if ((xi < x0) || (xi > x1)) {
    continue;
    }

Those output cells are never initialized.

Impact

Debug builds can fail on a valid four-input call because of the stale DCHECK_EQ(3, context->num_inputs()). Release builds can return undefined values for out-of-range x, making NVNMD quantization behavior depend on allocator contents.

Suggested fix

Remove or correct the stale DCHECK, convert shape/input assumptions to OP_REQUIRES, and define out-of-range behavior explicitly by clamping or zero-filling before the interval loop.

Add tests for a valid four-input call and for x values below the first interval and above the last interval.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    Status
    In Progress

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions