Skip to content

Conversation

@advaitjain
Copy link
Member

@advaitjain advaitjain commented Jan 7, 2021

Summary:

  • Move shared structs / helper functions into fully_connected_common.cc
  • Clean up some of the existing code to directly call the reference implementations (made possible by the refactor of the helper functions).

Also, this refactor addresses the sign flip in fully_connected: http://b/138810107

@google-ml-butler google-ml-butler bot added the size:L CL Change Size: Large label Jan 7, 2021
@google-ml-butler
Copy link

Thanks for contributing to TensorFlow Lite Micro.

To keep this process moving along, we'd like to make sure that you have completed the items on this list:

We would like to have a discussion on the Github issue first to determine the best path forward, and then proceed to the PR review.

@google-cla google-cla bot added the cla: yes label Jan 7, 2021
@advaitjain advaitjain added the comp:micro Related to TensorFlow Lite Microcontrollers label Jan 7, 2021
@advaitjain advaitjain requested a review from njeffrie January 7, 2021 00:47
…ized kernels.

This change is currently for discussion and to figure out what parts of
this refactor we like and what we do not.

Also note that we have `#if !defined(XTENSA)` in
fully_connected_common.cc because the linker appears to be failing to
discard the unused symbols. We will discuss this further with the
Cadence engineers but having some repro case that can be merged would
likely be useful.

TODO: make a github issue describing this linker behavior in more
detail.

Also, this refactor addresses the sign flip in fully_connected: http://b/138810107
@advaitjain advaitjain force-pushed the refactor-fully_connected branch from 55870c4 to 0a469be Compare January 7, 2021 00:52
context, sizeof(OpDataFullyConnectedReference));
}

#if !defined(XTENSA)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pnikam-cad @nyadla-sys @kpraving

These ifdefs are very undesirable but currently needed because the xtensa linker does not appear to be dropping unused symbols.

If I build the keyword_benchmark without this define the size is ~2.5 KB larger than if I keep this define. However, all the functions within this define are unused and I would expect the linker to be able to drop those symbols.

I will give more details tomorrow but wanted to flag this issue.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was user-error on my part -- I very likely missed a make clean.

Filed #46261 to fix the underlying cause.

@advaitjain advaitjain added the kokoro:force-run Tests on submitted change label Jan 7, 2021
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Jan 7, 2021
@advaitjain
Copy link
Member Author

@freddan80 @felix-johnny: we're trying to see what might work to increase code-sharing between reference and optimized kernels. Let us know if you have any suggestions.

@yair-ehrenwald: this PR is a stab at improved code sharing. Let us know if you have any suggestions.

@gbaned gbaned self-assigned this Jan 7, 2021
Copy link
Member Author

@advaitjain advaitjain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@njeffrie and I talked some more (see the review comments).

return EvalFloat(context, node, params->activation, input, filter, bias,
output);
return EvalFloatFullyConnectedReference(context, node, params->activation,
input, filter, bias, output);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably better to avoid the intermediate EvalFloatFullyConnectedReference altogether:

  tflite::reference_ops::FullyConnected(
      ToFloatParams(activation), tflite::micro::GetTensorShape(input),
      tflite::micro::GetTensorData<float>(input),
      tflite::micro::GetTensorShape(filter),
      tflite::micro::GetTensorData<float>(filter),
      tflite::micro::GetTensorShape(bias),
      tflite::micro::GetTensorData<float>(bias),
      tflite::micro::GetTensorShape(output),
      tflite::micro::GetTensorData<float>(output));

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

return kTfLiteOk;
}

TfLiteStatus EvalQuantizedInt8FullyConnectedReference(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove in favor of directly calling the reference implementation, now that the OpData to OpParams is a single function call.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
};

extern const int kFullyConnectedInputTensor;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we'll make these:

inline int fully_Connected_input_tensor_index() { return 0};

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kept as extern const.

}

#if !defined(XTENSA)
TfLiteStatus CalculateOpDataFullyConnectedReference(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we'll keep this function in common

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

return kTfLiteOk;
}

TfLiteStatus EvalQuantizedFullyConnectedReference(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will be removed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


namespace tflite {

struct OpDataFullyConnectedReference {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

call this OpDataFullyConnected (drop the reference suffix).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@advaitjain advaitjain force-pushed the refactor-fully_connected branch from 49ed518 to 14be7a3 Compare January 7, 2021 21:31
@advaitjain advaitjain added the kokoro:force-run Tests on submitted change label Jan 7, 2021
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Jan 7, 2021
@advaitjain advaitjain force-pushed the refactor-fully_connected branch from 14be7a3 to fa2d61e Compare January 7, 2021 21:46
@advaitjain advaitjain force-pushed the refactor-fully_connected branch from fa2d61e to bbd388b Compare January 7, 2021 23:30
@advaitjain advaitjain added the kokoro:force-run Tests on submitted change label Jan 7, 2021
@advaitjain
Copy link
Member Author

Ready for review again.

@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Jan 7, 2021
@google-ml-butler google-ml-butler bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Jan 8, 2021
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Jan 8, 2021
@freddan80
Copy link
Contributor

@advaitjain I'll havbe a look at it today

Copy link
Contributor

@freddan80 freddan80 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks nice! I have a minor clean-up comment.

TfLiteStatus CalculateOpData(TfLiteContext* context,
TfLiteFusedActivation activation,
TfLiteType data_type, const TfLiteTensor* input,
const TfLiteTensor* filter,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CalculateOpData() can be removed if we propagate "data->buffer_idx = -1;" into Prepare().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

@google-ml-butler google-ml-butler bot removed the ready to pull PR ready for merge process label Jan 12, 2021
@advaitjain advaitjain force-pushed the refactor-fully_connected branch from a2a53b8 to c8bc253 Compare January 12, 2021 04:57
@advaitjain advaitjain added the kokoro:force-run Tests on submitted change label Jan 12, 2021
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Jan 12, 2021
@advaitjain advaitjain added ready to pull PR ready for merge process kokoro:force-run Tests on submitted change and removed ready to pull PR ready for merge process labels Jan 12, 2021
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Jan 12, 2021
@copybara-service copybara-service bot merged commit 88e5764 into tensorflow:master Jan 12, 2021
@advaitjain advaitjain deleted the refactor-fully_connected branch January 12, 2021 23:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes comp:micro Related to TensorFlow Lite Microcontrollers ready to pull PR ready for merge process size:L CL Change Size: Large

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants