Consider the following two functions $f,g$:
$$ \begin{aligned} f(k,p,x)&=kx^2p^{-1} \\ g(k,m,x)&=2^mxk^{-1}\;\text{.} \end{aligned} $$
While both functions share the parameter $k$, they do not share $p,m$. The partial derivatives for both functions are:
$$ \begin{aligned} \nabla\,f=& \bigg[\;\frac{\partial\,f}{\partial\,k}=x^2p^{-1},\; \frac{\partial\,f}{\partial\,p}=-\frac{kx^2}{p^2},\; \frac{\partial\,f}{\partial\,m}=0\;\bigg]^\top\;\text{, and for }g(x)\text{:} \\[2em] \nabla\,g=& \bigg[\;\frac{\partial\,g}{\partial\,k}=-\frac{x2^m}{k^2},\; \frac{\partial\,g}{\partial\,p}=0,\; \frac{\partial\,g}{\partial\,m}=2^mx\log{(2)}k^{-1}\;\bigg]^\top\;\text{.} \end{aligned} $$
As can be seen, the partial derivatives for $\frac{\partial\,f}{\partial\,m}$ and $\frac{\partial\,g}{\partial\,p}$ are $0$, as the respective parameters do not exist in the respective functions.
Problem: The chain rule tells us that the derivative of the composite function $h(\dots,\,x)=f(g(x))$ is $h'(k,m,p,x)=f'(\dots,\;g(\dots,x))\times g'(\dots,\;x)$. Using the partial derivatives defined previously, this will lead to the partial derivatives for $p,m$ of the composite function $h(\dots,\,x)$ to also be zero.
However, if we do a manual substitution of $g(\dots,\,x)$ into $f(\dots,\,x)$, the resulting composite function becomes:
$$ \begin{aligned} h(k,m,p,x)&=f(k,p,g(k,m,x)) \\ &=\frac{k}{p}\times\bigg(\frac{x2^m}{k}\bigg)^2\;\text{,} \end{aligned} $$
which can obviously be derived for all parameters with a non-zero result. So where have I gone wrong here? Is the composition of $h(x)$ wrong?
Addendum: The partial derivatives of the composite function and the manual application of the chain rule for parameter $k$ are also not identical:
$$ \begin{aligned} \frac{\partial\,h}{\partial\,k}&=-\frac{4^mx^2}{k^2p}\;\text{, and using the chain rule it is:} \\ \text{chain rule}&=-\frac{2^{3m}x^3}{k^4p}\;\text{.} \end{aligned} $$
I do not understand where the problem is, any directions are welcome!